home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / frasr182.zip / MISCFRAC.C < prev    next >
C/C++ Source or Header  |  1993-07-13  |  59KB  |  2,169 lines

  1. /*
  2.  
  3. Miscellaneous fractal-specific code (formerly in CALCFRAC.C)
  4.  
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <float.h>
  12. #ifndef XFRACT
  13. #include <dos.h>
  14. #endif
  15. #include <limits.h>
  16. #include "fractint.h"
  17. #include "fractype.h"
  18. #include "mpmath.h"
  19. #include "targa_lc.h"
  20. #include "prototyp.h"
  21.  
  22. /* routines in this module    */
  23.  
  24. static void set_Plasma_palette(void);
  25. static U16 _fastcall adjust(int xa,int ya,int x,int y,int xb,int yb);
  26. static void _fastcall subDivide(int x1,int y1,int x2,int y2);
  27. static int _fastcall new_subD (int x1,int y1,int x2,int y2, int recur);
  28. static void verhulst(void);
  29. static void Bif_Period_Init(void);
  30. static int  _fastcall Bif_Periodic(int);
  31. static void set_Cellular_palette(void);
  32.  
  33. U16  (_fastcall *getpix)(int,int)  = (U16(_fastcall *)(int,int))getcolor;
  34.  
  35. extern int resuming;
  36. extern char stdcalcmode;
  37. extern int color, oldcolor, row, col, passes;
  38. extern int ixstart, ixstop, iystart, iystop;
  39. extern _CMPLX init,tmp,old,new,saved;
  40. extern _CMPLX parm,parm2;
  41.  
  42. extern double far *dx0, far *dy0;
  43. extern double far *dx1, far *dy1;
  44. extern long far *lx0, far *ly0;
  45. extern long far *lx1, far *ly1;
  46. extern long delx, dely;
  47. extern double deltaX, deltaY;
  48. extern int sxoffs, syoffs, sxdots, sydots;
  49. extern int xdots, ydots;
  50. extern int maxit, inside, colors, andcolor, dotmode;
  51. extern double param[];
  52. extern int rflag, rseed;
  53. extern int pot16bit, potflag;
  54. extern int diskvideo;
  55. extern int bitshift;
  56. extern long fudge;
  57. extern int show_orbit;
  58. extern int periodicitycheck, integerfractal;
  59. extern _LCMPLX linit;
  60. extern _LCMPLX ltmp;
  61. extern _LCMPLX lold,lnew,lparm,lparm2;
  62. extern long ltempsqrx,ltempsqry;
  63. extern double tempsqrx,tempsqry;
  64. extern int overflow;
  65. extern int kbdcount, max_kbdcount;
  66. extern int reset_periodicity;
  67. extern int calc_status;
  68. extern int iterations, invert;
  69. extern int save_release;
  70. extern int LogFlag;
  71. extern int (calctype());
  72. extern int realcolor;
  73. extern int nxtscreenflag;
  74. extern double magnitude, rqlim, rqlim2, rqlim_save;
  75. extern long lmagnitud, llimit, llimit2, lclosenuff, l16triglim;
  76. extern int orbit_color, orbit_ptr, showdot;
  77. extern int debugflag;
  78.  
  79. #ifndef XFRACT
  80. extern char dstack[4096];
  81. extern char boxy[4096];
  82. #else
  83. BYTE dstack[4096];
  84. #endif
  85.  
  86. typedef void (_fastcall *PLOT)(int,int,int);
  87.  
  88. /***************** standalone engine for "test" ********************/
  89.  
  90. int test()
  91. {
  92.    int startrow,startpass,numpasses;
  93.    startrow = startpass = 0;
  94.    if (resuming)
  95.    {
  96.       start_resume();
  97.       get_resume(sizeof(int),&startrow,sizeof(int),&startpass,0);
  98.       end_resume();
  99.    }
  100.    if(teststart()) /* assume it was stand-alone, doesn't want passes logic */
  101.       return(0);
  102.    numpasses = (stdcalcmode == '1') ? 0 : 1;
  103.    for (passes=startpass; passes <= numpasses ; passes++)
  104.    {
  105.       for (row = startrow; row <= iystop; row=row+1+numpasses)
  106.       {
  107.      register int col;
  108.      for (col = 0; col <= ixstop; col++)       /* look at each point on screen */
  109.      {
  110.         register color;
  111.         init.y = dy0[row]+dy1[col];
  112.         init.x = dx0[col]+dx1[row];
  113.         if(check_key())
  114.         {
  115.            testend();
  116.            alloc_resume(20,1);
  117.            put_resume(sizeof(int),&row,sizeof(int),&passes,0);
  118.            return(-1);
  119.         }
  120.         color = testpt(init.x,init.y,parm.x,parm.y,maxit,inside);
  121.         if (color >= colors) /* avoid trouble if color is 0 */
  122.            if (colors < 16)
  123.           color &= andcolor;
  124.            else
  125.           color = ((color-1) % andcolor) + 1; /* skip color zero */
  126.         (*plot)(col,row,color);
  127.         if(numpasses && (passes == 0))
  128.            (*plot)(col,row+1,color);
  129.      }
  130.       }
  131.       startrow = passes + 1;
  132.    }
  133.    testend();
  134.    return(0);
  135. }
  136.  
  137. /***************** standalone engine for "plasma" ********************/
  138.  
  139. static int iparmx;      /* iparmx = parm.x * 16 */
  140. static int shiftvalue;  /* shift based on #colors */
  141. extern int max_colors;
  142. static int recur1=1;
  143. static int pcolors;
  144. static int recur_level = 0;
  145. U16 max_plasma;
  146.  
  147. /* returns a random 16 bit value that is never 0 */
  148. U16 rand16()
  149. {
  150.    U16 value;
  151.    value = rand15();
  152.    value <<= 1;
  153.    value += rand15()&1;
  154.    if(value < 1)
  155.       value = 1;
  156.    return(value);
  157. }
  158.  
  159. void _fastcall putpot(int x, int y, U16 color)
  160. {
  161.    if(color < 1)
  162.       color = 1;
  163.    putcolor(x, y, color >> 8 ? color >> 8 : 1);  /* don't write 0 */
  164.    /* we don't write this if dotmode==11 because the above putcolor
  165.          was already a "writedisk" in that case */
  166.    if (dotmode != 11)
  167.       writedisk(x+sxoffs,y+syoffs,color >> 8);    /* upper 8 bits */
  168.    writedisk(x+sxoffs,y+sydots+syoffs,color&255); /* lower 8 bits */
  169. }
  170.  
  171. U16 _fastcall getpot(int x, int y)
  172. {
  173.    U16 color;
  174.  
  175.    color = (U16)readdisk(x+sxoffs,y+syoffs);
  176.    color = (color << 8) + (U16) readdisk(x+sxoffs,y+sydots+syoffs);
  177.    return(color);
  178. }
  179.  
  180.  
  181. typedef struct palett
  182. {
  183.    BYTE red;
  184.    BYTE green;
  185.    BYTE blue;
  186. }
  187. Palettetype;
  188.  
  189. extern Palettetype dacbox[256];
  190. static int plasma_check;                        /* to limit kbd checking */
  191.  
  192. static U16 _fastcall adjust(int xa,int ya,int x,int y,int xb,int yb)
  193. {
  194.    S32 pseudorandom;
  195.    pseudorandom = ((S32)iparmx)*((rand15()-16383));
  196. /*   pseudorandom = pseudorandom*(abs(xa-xb)+abs(ya-yb));*/
  197.    pseudorandom = pseudorandom * recur1;
  198.    pseudorandom = pseudorandom >> shiftvalue;
  199.    pseudorandom = (((S32)getpix(xa,ya)+(S32)getpix(xb,yb)+1)>>1)+pseudorandom;
  200.    if(max_plasma == 0)
  201.    {
  202.       if (pseudorandom >= pcolors)
  203.          pseudorandom = pcolors-1;
  204.    }
  205.    else if (pseudorandom >= max_plasma)
  206.       pseudorandom = max_plasma;
  207.    if(pseudorandom < 1)
  208.       pseudorandom = 1;
  209.    plot(x,y,(U16)pseudorandom);
  210.    return((U16)pseudorandom);
  211. }
  212.  
  213.  
  214. static int _fastcall new_subD (int x1,int y1,int x2,int y2, int recur)
  215. {
  216.    int x,y;
  217.    int nx1;
  218.    int nx;
  219.    int ny1, ny;
  220.    S32 i, v;
  221.  
  222.    struct sub {
  223.       BYTE t; /* top of stack */
  224.       int v[16]; /* subdivided value */
  225.       BYTE r[16];  /* recursion level */
  226.    };
  227.  
  228.    static struct sub subx, suby;
  229.  
  230.    /*
  231.    recur1=1;
  232.    for (i=1;i<=recur;i++)
  233.       recur1 = recur1 * 2;
  234.    recur1=320/recur1;
  235.    */
  236.    recur1 = 320L >> recur;
  237.    suby.t = 2;
  238.    ny   = suby.v[0] = y2;
  239.    ny1 = suby.v[2] = y1;
  240.    suby.r[0] = suby.r[2] = 0;
  241.    suby.r[1] = 1;
  242.    y = suby.v[1] = (ny1 + ny) >> 1;
  243.  
  244.    while (suby.t >= 1)
  245.    {
  246.       if ((++plasma_check & 0x0f) == 1)
  247.          if(check_key())
  248.          {
  249. /*   naah, we don't want to flush this key!!!
  250.                         getch();
  251.             */
  252.             plasma_check--;
  253.             return(1);
  254.          }
  255.       while (suby.r[suby.t-1] < recur)
  256.       {
  257.          /*     1.  Create new entry at top of the stack  */
  258.          /*     2.  Copy old top value to new top value.  */
  259.          /*            This is largest y value.           */
  260.          /*     3.  Smallest y is now old mid point       */
  261.          /*     4.  Set new mid point recursion level     */
  262.          /*     5.  New mid point value is average        */
  263.          /*            of largest and smallest            */
  264.  
  265.          suby.t++;
  266.          ny1  = suby.v[suby.t] = suby.v[suby.t-1];
  267.          ny   = suby.v[suby.t-2];
  268.          suby.r[suby.t] = suby.r[suby.t-1];
  269.          y    = suby.v[suby.t-1]   = (ny1 + ny) >> 1;
  270.          suby.r[suby.t-1]   = (int)max(suby.r[suby.t], suby.r[suby.t-2])+1;
  271.       }
  272.       subx.t = 2;
  273.       nx  = subx.v[0] = x2;
  274.       nx1 = subx.v[2] = x1;
  275.       subx.r[0] = subx.r[2] = 0;
  276.       subx.r[1] = 1;
  277.       x = subx.v[1] = (nx1 + nx) >> 1;
  278.  
  279.       while (subx.t >= 1)
  280.       {
  281.          while (subx.r[subx.t-1] < recur)
  282.          {
  283.             subx.t++; /* move the top ofthe stack up 1 */
  284.             nx1  = subx.v[subx.t] = subx.v[subx.t-1];
  285.             nx   = subx.v[subx.t-2];
  286.             subx.r[subx.t] = subx.r[subx.t-1];
  287.             x    = subx.v[subx.t-1]   = (nx1 + nx) >> 1;
  288.             subx.r[subx.t-1]   = (int)max(subx.r[subx.t],
  289.                 subx.r[subx.t-2])+1;
  290.          }
  291.  
  292.          if ((i = getpix(nx, y)) == 0)
  293.             i = adjust(nx,ny1,nx,y ,nx,ny);
  294.          v = i;
  295.          if ((i = getpix(x, ny